<?php

namespace Abzu\ArticlesBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Abzu\ArticlesBundle\Entity\AbzuArticle;
use Abzu\ArticlesBundle\Form\AbzuArticleType;
use JMS\SecurityExtraBundle\Annotation\Secure; 


class ArticlesController extends Controller
{
    
    public function indexAction()
    {
        $em = $this->getDoctrine()->getEntityManager();
        $articles = $em->getRepository('AbzuArticlesBundle:AbzuArticle')
                ->findAll();

        return $this->render('AbzuArticlesBundle:Articles:index.html.twig', 
                array('articles' => $articles));
    
    }
    
    
    /**
     * Show an article
     */
    public function showAction($id, $slug) {
        $entityManager = $this->getDoctrine()->getEntityManager();
        $article = $entityManager->getRepository('AbzuArticlesBundle:AbzuArticle')->find($id);

        if (!$article) {
            throw $this->createNotFoundException('Not found the article id: ' . $id);
        }
        

        return $this->render(
                'AbzuArticlesBundle:Articles:show.html.twig', 
                array('article' => $article ));
    }

    /**
     * @Secure(roles="ROLE_ADMIN")
     * @return type 
     */
    public function newAction() {
        $article = new AbzuArticle();
        $form = $this->createForm(new AbzuArticleType(), $article);
        return $this->render('AbzuArticlesBundle:Articles:new.html.twig', 
                array('form' => $form->createView(), 'article' => $article));
    }

    /**
     * @Secure(roles="ROLE_ADMIN")
     * @param type $id
     * @return type 
     */
    public function editAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $article = $em->getRepository('AbzuArticlesBundle:AbzuArticle')->find($id);
        if (!$article) {
            throw $this->createNotFoundException('Article not found. Unknown id (' . $id . ')');
        }

        $form = $this->createForm(new AbzuArticleType(), $article);
        return $this->render('AbzuArticlesBundle:Articles:edit.html.twig', 
                array('form' => $form->createView(), 'article' => $article));
    }

    /**
     * @Secure(roles="ROLE_ADMIN")
     * @return type 
     */
    public function createAction() {
        $article = new AbzuArticle();
        $request = $this->getRequest();
        $form = $this->createForm(new AbzuArticleType(), $article);
        $form->bindRequest($request);
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($article);
            $em->flush();

            $this->get('session')->setFlash('notice', 'The article was successfully created');
            return $this->redirect(
                            $this->generateUrl('AbzuArticlesBundle_articles_edit', 
                                    array('id' => $article->getId()))
            );
            
        }
        return $this->render('AbzuArticlesBundle:Articles:new.html.twig', 
                array('form' => $form->createView(), 'article' => $article));
    }

    /**
     * @Secure(roles="ROLE_ADMIN")
     * @param type $id
     * @return type 
     */
    public function updateAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $article = $em->getRepository('AbzuArticlesBundle:AbzuArticle')->find($id);
        $request = $this->getRequest();
        $form = $this->createForm(new AbzuArticleType(), $article);
        $form->bindRequest($request);
        if ($form->isValid()) {
            $em->persist($article);
            $em->flush();

            $this->get('session')->setFlash('notice', 'The article was saved successfully');
        }
        return $this->render('AbzuArticleBundle:Articles:edit.html.twig', 
                array('form' => $form->createView(), 'article' => $article));
    }

    
}
